package gisdcl.geoprocessors;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.util.logging.Logger;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

public class JTSEngine {
	private static final Logger log = Logger.getLogger(JTSEngine.class.getName());
	
	
	public boolean isAddressInDistrict(double pLat, double pLon , String pPolygonAsWKT) throws Exception{
		log.info("Geoprocessing Engine ...............");
		log.info("pLat : " + pLat);
		log.info("pLon : " + pLon);
		log.info("pPolygonAsWKT : " + pPolygonAsWKT);
		log.info("pPolygonAsWKT.length(): " + pPolygonAsWKT.length());
		try {
			GeometryFactory gf = new GeometryFactory();
			WKTReader reader = new WKTReader( gf );
			
			Polygon polygon = (Polygon) reader.read(pPolygonAsWKT); 
			log.info("polygon: " + polygon.toString());
			 
			//Coordinate coord = new Coordinate( -83.24758359372339, 42.53284873074468 );
			Coordinate coord = new Coordinate( pLon,pLat );
			log.info("coord.toString() : " + coord.toString());
			Point point = gf.createPoint( coord );
			log.info("point.toString() : " + point.toString());
			
			Boolean isPointInPolygon = polygon.contains(point);
			log.info("isPointInPolygon: " + isPointInPolygon.toString());
			
			return isPointInPolygon;
			
		} catch (ParseException e) {
			log.info("ParseException");
			log.warning(e.getMessage());
			throw e;
		} catch (Exception e){
			log.info("Exception");
			log.info(e.getMessage());
			throw e;
		}
		
	}
	
	public String testPointInPolygon() throws ParseException{
		
		try {
			GeometryFactory gf = new GeometryFactory();
			WKTReader reader = new WKTReader( gf );
			
			//Polygon polygon = (Polygon) reader.read("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))");
			Polygon polygon = (Polygon) reader.read(" POLYGON ((-83.32200399999999 42.571565, -83.322536 42.571872, -83.32354699999999 42.572455999999995, -83.325183 42.573402, -83.323652 42.573338, -83.322606 42.573294, -83.321085 42.573229999999995, -83.320785 42.573287, -83.320481 42.573201, -83.317234 42.572433, -83.314634 42.572433, -83.313133 42.572533, -83.309542 42.572513, -83.30788 42.572503999999995, -83.307897 42.571182, -83.306833 42.571233, -83.303133 42.571433, -83.303133 42.570633, -83.302933 42.570133, -83.303133 42.569632999999996, -83.302933 42.568433, -83.30283299999999 42.567032999999995, -83.304453 42.567043999999996, -83.306794 42.567059, -83.306799 42.566174, -83.306833 42.565033, -83.306833 42.564633, -83.308048 42.564664, -83.308064 42.559711, -83.308033 42.558133, -83.307034 42.558034, -83.306928 42.554634, -83.306845 42.551966, -83.306604 42.544233999999996, -83.307932 42.544233999999996, -83.310232 42.544033999999996, -83.310243 42.541478999999995, -83.31024699999999 42.540575, -83.310262 42.536843, -83.310687 42.536840999999995, -83.312516 42.536834, -83.31253199999999 42.535633999999995, -83.31253199999999 42.533434, -83.31244699999999 42.532837, -83.312426 42.530046, -83.31242 42.529233999999995, -83.311432 42.529233999999995, -83.308432 42.529534, -83.305032 42.529534, -83.303832 42.529534, -83.300332 42.529634, -83.29933199999999 42.529734, -83.29753199999999 42.529734, -83.297232 42.529734, -83.29673199999999 42.529734, -83.294932 42.529834, -83.29468299999999 42.529834, -83.294232 42.529834, -83.29253299999999 42.529734999999995, -83.291961 42.529762999999996, -83.290431 42.529838, -83.290331 42.529934, -83.289431 42.529934, -83.28893099999999 42.529934, -83.288431 42.529934, -83.28763099999999 42.529934, -83.286031 42.529934, -83.28473199999999 42.529934999999995, -83.283063 42.530077999999996, -83.281331 42.530234, -83.280031 42.530234, -83.278731 42.530333999999996, -83.275746 42.530318, -83.274073 42.530363, -83.274103 42.533071, -83.274131 42.535534, -83.274231 42.536834, -83.276786 42.536775, -83.276083 42.537974999999996, -83.275931 42.538233999999996, -83.275331 42.539134, -83.276033 42.539539, -83.27573699999999 42.539896999999996, -83.276703 42.540315, -83.276732 42.545034, -83.275099 42.549025, -83.268762 42.549144, -83.268784 42.552811, -83.265023 42.552819, -83.26339999999999 42.552822, -83.26343299999999 42.553335, -83.263632 42.554534, -83.26373199999999 42.554933999999996, -83.26373199999999 42.555534, -83.26373199999999 42.555834, -83.26373199999999 42.556833999999995, -83.263758 42.557441999999995, -83.263786 42.558088999999995, -83.263832 42.559134, -83.26203199999999 42.559134, -83.26095699999999 42.559183, -83.259832 42.559234, -83.259232 42.559234, -83.25751699999999 42.559293, -83.256332 42.559334, -83.255432 42.559334, -83.25523199999999 42.559334, -83.254032 42.559334, -83.25133199999999 42.559433999999996, -83.25053299999999 42.559435, -83.249568 42.559481999999996, -83.249585 42.559889999999996, -83.249586 42.561202, -83.248443 42.5612, -83.245806 42.561197, -83.245783 42.562225, -83.245763 42.563119, -83.245763 42.564004, -83.246961 42.564263, -83.247523 42.564384, -83.24754999999999 42.565844, -83.247565 42.566665, -83.24757799999999 42.567408, -83.246732 42.567434, -83.246588 42.567442, -83.246646 42.570353, -83.246675 42.571832, -83.246589 42.571850999999995, -83.24601899999999 42.571974, -83.24471 42.572257, -83.24445999999999 42.571934, -83.244332 42.571934, -83.243432 42.571134, -83.242932 42.570733999999995, -83.23904399999999 42.570688, -83.238832 42.570333999999995, -83.23883099999999 42.567533999999995, -83.237631 42.567533999999995, -83.237259 42.567529, -83.237231 42.568633999999996, -83.237269 42.569286, -83.235873 42.570024, -83.23564999999999 42.570142, -83.23407499999999 42.569182999999995, -83.23407499999999 42.568934, -83.23407499999999 42.567488999999995, -83.231282 42.567454, -83.231293 42.569089, -83.232432 42.570235, -83.232574 42.570386, -83.23308 42.570924, -83.23319 42.571041, -83.233418 42.571283, -83.23430599999999 42.572227, -83.234496 42.572427999999995, -83.234878 42.572834, -83.233739 42.573423, -83.233329 42.572990999999995, -83.232283 42.571889999999996, -83.232098 42.571967, -83.23039299999999 42.572348999999996, -83.229047 42.571616, -83.225815 42.572773999999995, -83.225832 42.573235, -83.225961 42.575311, -83.22488299999999 42.575328, -83.224531 42.575334, -83.221131 42.575434, -83.221231 42.576034, -83.221231 42.576733999999995, -83.22133099999999 42.578832999999996, -83.22133099999999 42.580532999999996, -83.221531 42.581333, -83.221631 42.582333, -83.221631 42.582977, -83.22042599999999 42.583, -83.21873099999999 42.583033, -83.217331 42.583033, -83.21613099999999 42.583133, -83.213531 42.582933, -83.210428 42.582906, -83.20823399999999 42.582887, -83.20726599999999 42.582879, -83.20723199999999 42.582133999999996, -83.203569 42.582139999999995, -83.201331 42.582133, -83.198928 42.582203, -83.19892399999999 42.581945999999995, -83.198909 42.580934, -83.19888499999999 42.579338, -83.19866499999999 42.579335, -83.19724699999999 42.579316999999996, -83.197077 42.575831, -83.19682999999999 42.574434, -83.19653 42.573434, -83.19642999999999 42.573034, -83.19663 42.572233999999995, -83.19673 42.571334, -83.19663 42.570333999999995, -83.19693 42.569834, -83.19682999999999 42.568433999999996, -83.19693 42.566534, -83.196896 42.565473, -83.196888 42.564533, -83.196877 42.564082, -83.196849 42.56325, -83.19676799999999 42.560842, -83.196722 42.559487, -83.196607 42.558555, -83.19630599999999 42.556115999999996, -83.196231 42.556135, -83.196231 42.555234999999996, -83.19633 42.554834, -83.19613 42.553934, -83.193029 42.553934, -83.189628 42.553987, -83.18642899999999 42.554134, -83.186229 42.553233999999996, -83.186229 42.552234, -83.186229 42.551334, -83.186229 42.550433999999996, -83.186229 42.549333999999995, -83.186129 42.548234, -83.186129 42.547334, -83.185998 42.543518, -83.182711 42.543597, -83.177137 42.543731, -83.176037 42.543757, -83.17604 42.543455, -83.176071 42.540637, -83.176079 42.539932, -83.17777799999999 42.539905, -83.178268 42.539902999999995, -83.18578 42.539595999999996, -83.18572999999999 42.538934999999995, -83.18543 42.535534999999996, -83.18533 42.535035, -83.185328 42.532433999999995, -83.185628 42.532433999999995, -83.18682799999999 42.532433999999995, -83.188028 42.532233999999995, -83.189329 42.532233999999995, -83.19023 42.532135, -83.19143 42.532135, -83.19212999999999 42.532135, -83.19342999999999 42.532035, -83.19462999999999 42.532035, -83.19592999999999 42.532035, -83.19673 42.531935, -83.19953 42.531835, -83.20412999999999 42.531735, -83.204229 42.531034, -83.204129 42.529934, -83.204129 42.529233999999995, -83.204129 42.528434, -83.20402899999999 42.527634, -83.20402899999999 42.526834, -83.20402899999999 42.526134, -83.203929 42.525134, -83.203929 42.524634, -83.203929 42.524034, -83.203929 42.523134, -83.203929 42.522234, -83.203929 42.521434, -83.203818 42.520432, -83.203729 42.519633999999996, -83.203828 42.517434, -83.203628 42.514734999999995, -83.20352799999999 42.512335, -83.203428 42.510034999999995, -83.204428 42.510034999999995, -83.205334 42.510028999999996, -83.205355 42.510259999999995, -83.206532 42.510028, -83.207593 42.510039, -83.213094 42.509949, -83.214919 42.50993, -83.215542 42.509923, -83.216154 42.509917, -83.216792 42.50991, -83.21743099999999 42.509903, -83.21807199999999 42.509896, -83.218049 42.510422, -83.218029 42.510875, -83.21813 42.512983, -83.218183 42.514081, -83.218214 42.514734, -83.21825199999999 42.515533999999995, -83.21829 42.516324, -83.218329 42.517134, -83.220529 42.517033999999995, -83.223129 42.516934, -83.225116 42.516881999999995, -83.226657 42.516841, -83.226929 42.516833999999996, -83.228169 42.516752, -83.22815299999999 42.516141, -83.22801799999999 42.510985, -83.22856399999999 42.510988, -83.232626 42.511010999999996, -83.233569 42.511016, -83.234856 42.511023, -83.235688 42.511027999999996, -83.236729 42.511033999999995, -83.236729 42.510334, -83.236582 42.508896, -83.236567 42.508257, -83.237568 42.508283999999996, -83.240246 42.508221, -83.241945 42.508212, -83.24193 42.507636, -83.241868 42.506834, -83.24183 42.506236, -83.24181899999999 42.506004999999995, -83.24690799999999 42.506101, -83.24691299999999 42.50972, -83.24793 42.509645, -83.249763 42.50951, -83.251142 42.509402, -83.252594 42.509279, -83.25259199999999 42.508843999999996, -83.252583 42.507217, -83.253536 42.507208999999996, -83.25502399999999 42.507197, -83.254993 42.508891999999996, -83.255495 42.508852, -83.257345 42.508742, -83.257358 42.508807999999995, -83.25738 42.508917, -83.257851 42.508914999999995, -83.259365 42.508831, -83.260004 42.508828, -83.260728 42.508798999999996, -83.262031 42.508735, -83.264231 42.508635, -83.26494699999999 42.508635, -83.26633 42.508634, -83.26813 42.508534, -83.26773 42.507833999999995, -83.26823 42.507433999999996, -83.26742999999999 42.507234, -83.26853 42.505734, -83.26983 42.505334, -83.27042999999999 42.505734, -83.27153 42.504433999999996, -83.272154 42.504433999999996, -83.27363 42.504433999999996, -83.27562999999999 42.504433999999996, -83.27573 42.505334, -83.27593 42.508434, -83.27823 42.508334, -83.280452 42.508238999999996, -83.285232 42.508035, -83.294131 42.507833999999995, -83.299432 42.507735, -83.30103199999999 42.507635, -83.309731 42.507433999999996, -83.315831 42.507234, -83.319531 42.507034, -83.319631 42.508834, -83.31983199999999 42.514534, -83.320032 42.514834, -83.319632 42.518133999999996, -83.319932 42.520334, -83.319932 42.521834, -83.320132 42.522434, -83.320033 42.523435, -83.320033 42.523734999999995, -83.320132 42.524733999999995, -83.320132 42.526033999999996, -83.320132 42.527533999999996, -83.320033 42.529035, -83.321732 42.529134, -83.32283199999999 42.529134, -83.324832 42.529033999999996, -83.32713199999999 42.531833999999996, -83.325538 42.532351, -83.325535 42.533435999999995, -83.325533 42.53416, -83.325533 42.534734, -83.325594 42.536226, -83.326933 42.536234, -83.335833 42.535969, -83.338979 42.535889999999995, -83.340233 42.535858999999995, -83.340233 42.538233999999996, -83.340233 42.539533, -83.340633 42.543233, -83.340633 42.545133, -83.34083299999999 42.549433, -83.34083299999999 42.550633, -83.34133399999999 42.555733, -83.341135 42.556334, -83.342489 42.556818, -83.344442 42.557514999999995, -83.34533499999999 42.557834, -83.346835 42.557733999999996, -83.347003 42.559926, -83.347037 42.563424999999995, -83.347043 42.564068999999996, -83.34705199999999 42.564934, -83.347054 42.565128, -83.34486 42.565194, -83.344634 42.565233, -83.340519 42.5652, -83.339562 42.565174999999996, -83.33637499999999 42.563496, -83.334296 42.5624, -83.327691 42.562356, -83.327648 42.565073, -83.325259 42.565048999999995, -83.32523599999999 42.566632999999996, -83.325205 42.568798, -83.324788 42.568774, -83.323212 42.568684, -83.322335 42.568633999999996, -83.32003499999999 42.570510999999996, -83.32200399999999 42.571565))");
			Double polyArea = polygon.getArea();

			Coordinate coord = new Coordinate( -83.24758359372339, 42.53284873074468 );
			
			Point point = gf.createPoint( coord );
			
			Boolean isPointInPolygon = polygon.contains(point);
			
			return isPointInPolygon.toString();
			
		} catch (ParseException e) {
			log.warning(e.getMessage());
			throw e;
		}
	}
	
	public String testJTS() throws ParseException {
		try {
			Geometry g1 = new WKTReader()
					.read("LINESTRING (0 0, 10 10, 20 20)");
			Coordinate[] coordinates = new Coordinate[] { new Coordinate(0, 0),
					new Coordinate(10, 10), new Coordinate(20, 20) };
			Geometry g2 = new GeometryFactory().createLineString(coordinates);
			//Geometry g3 = g1.intersection(g2);
			Double length = g2.getLength();
			return length.toString();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.warning(e.getMessage());
			throw e;
		}
	}
}
